﻿using System;
using UnityEngine;

[ExecuteInEditMode, RequireComponent(typeof(ParticleSystem))]
public class ParticleLookAtVelocity : MonoBehaviour
{
    private ParticleSystem.Particle[] particles = new ParticleSystem.Particle[0];
    private ParticleSystem ps;

    private void Awake()
    {
        this.ps = base.GetComponent<ParticleSystem>();
    }

    private void Update()
    {
        if (this.particles.Length < this.ps.maxParticles)
        {
            Array.Resize<ParticleSystem.Particle>(ref this.particles, this.ps.maxParticles);
        }
        int particles = this.ps.GetParticles(this.particles);
        for (int i = 0; i < particles; i++)
        {
            this.particles[i].rotation3D = Quaternion.LookRotation(this.particles[i].velocity).eulerAngles;
        }
        this.ps.SetParticles(this.particles, particles);
    }
}

